import { defHttp } from '/@/utils/http/axios';
import {
  UpdatePasswordModel,
  UserIdModel,
  UserListModel,
  UserListQueryParam,
  UserSaveModel,
  UserUpdateModel,
  UserUpdateRoleModel,
} from '../model';
import { IdModel } from '/@/api/model/baseModel';

enum Api {
  GetList = '/user/find',
  All = '/user/all',
  GetRoleIdList = '/user/roleIdList',
  Save = '/user/save',
  Update = '/user/update',
  Del = '/user/delete',
  UpdateRole = '/user/update/role',
  UpdatePassword = '/user/update/password',
}

/**
 * @description: 用户列表
 */
export const getList = (params: UserListQueryParam) => {
  return defHttp.get<UserListModel>({ url: Api.GetList, params });
};

/**
 * @description: 保存用户
 */
export const save = (params: UserSaveModel) => {
  return defHttp.post<UserSaveModel>({
    url: Api.Save,
    params,
  });
};

/**
 * @description: 更新用户信息
 */
export const update = (params: UserUpdateModel) => {
  return defHttp.post<UserUpdateModel>({
    url: Api.Update,
    params,
  });
};

/**
 * @description: 删除用户
 */
export const del = (params: IdModel) => {
  return defHttp.delete<IdModel>({
    url: `${Api.Del}?id=${params.id}`,
  });
};

/**
 * @description: 查询用户角色
 */
export const getRoleIdList = (params: UserIdModel) => {
  return defHttp.get<string[]>({ url: Api.GetRoleIdList, params });
};

/**
 * @description: 用户关联角色
 */
export const updateRole = (params: UserUpdateRoleModel) => {
  return defHttp.post<UserUpdateModel>({
    url: Api.UpdateRole,
    params,
  });
};

/**
 * @description: 用户列表
 */
export const getAll = async (params: any) => {
  return defHttp.post<UserListModel[]>({ url: Api.All, params });
};

/**
 * @description: 修改密码
 */
export const updatePassword = (params: UpdatePasswordModel) => {
  return defHttp.post<any>({
    url: Api.UpdatePassword,
    params,
  });
};
